package cn.edu.ncut.cs.springboot.springbootmybatisplus.controller;

import cn.edu.ncut.cs.springboot.springbootmybatisplus.entity.Course;
import cn.edu.ncut.cs.springboot.springbootmybatisplus.entity.Student;
import cn.edu.ncut.cs.springboot.springbootmybatisplus.service.ICourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ncut
 * @since 2024-12-07
 */
@Controller
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private ICourseService courseService;
    @RequestMapping("/listCourse")
    public String listCourse( Model model,@RequestParam(required = false) String cname) {
        QueryWrapper<Course> qw = new QueryWrapper<>();
        if (cname != null && !cname.isEmpty()) {
            qw.like("cname", cname);
        }
        List<Course> list = courseService.list(qw);
        model.addAttribute("courses", list);
        return "admin-course-list";
    }
    @RequestMapping("saveCourse")
    public String saveCourse(Course course){
        boolean b=courseService.save(course);
        return "redirect:/course/listCourse";
    }
    @RequestMapping("preUpdateCourse/{cno}")
    public String preUpdateCourse(@PathVariable String cno, Model model){
        Course course=courseService.getById(cno);
        model.addAttribute("course",course);
        return "admin-course-update";
    }
    @RequestMapping("updateCourse")
    public String updateCourse(Course course){
        boolean b=courseService.updateById(course);
        return "redirect:/course/listCourse";
    }
    @RequestMapping("delCourse/{cno}")
    public String delCourse(@PathVariable String cno){
        boolean b=courseService.removeById(cno);
        return "redirect:/course/listCourse";
    }
}
